/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package cn.yzc.server.impl;

import cn.yzc.client.GoodsRemoteClient;
import cn.yzc.entity.OrderDetails;
import cn.yzc.entity.Orders;
import cn.yzc.mapper.OrdersMapper;
import cn.yzc.server.OrdersServer;

import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>Project: SpringColud - OrdersServerImpl</p>
 * <p>Powered by yzc On 2024-01-05 17:54:28</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Service
public class OrdersServerImpl implements OrdersServer {

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    GoodsRemoteClient goodsRemoteClient;

    @GlobalTransactional
    @Override
    public int insert(Orders orders) {
        List<OrderDetails> orderDetailsList = orders.getOrderDetailsList();
        String id = String.valueOf(UUID.randomUUID());
        orders.setId(id);
        orderDetailsList.forEach(orderDetails -> {
            goodsRemoteClient.update(orderDetails.getGoodId(), orderDetails.getNumber());
        });
        int rs =ordersMapper.insert(orders);
        if (rs>0){
            ordersMapper.insertDetailsList(id,orderDetailsList);
        }

        return rs;
    }
}
